import java.util.Arrays;

public class MyBitSet {
    public byte[] elem;
    public int CapCity;
    public MyBitSet() {
        elem=new byte[1];
    }
    public MyBitSet(int n) {
        elem=new byte[n/8+1];
    }

    /**
     * 添加元素到位图
     * @param val
     */
    public void set(int val) {
        if(val<0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        int ArrayIndex=val/8;
        int BitIndex=val%8;
        if(ArrayIndex>elem.length-1) {
            elem = Arrays.copyOf(elem,ArrayIndex+1);
        }
        elem[ArrayIndex]|=(1<<BitIndex);
        CapCity++;
    }

    /**
     * 判断位图中是否存在该元素
     * @param val
     * @return
     */
    public boolean get(int val) {
        if(val<0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        int ArrayIndex=val/8;
        int BitIndex=val%8;
        if(ArrayIndex>elem.length-1) {
            elem = Arrays.copyOf(elem,ArrayIndex+1);
        }
        if((elem[ArrayIndex]&=(1<<BitIndex))!=0) {
            return true;
        }
        return false;
    }

    /**
     * 重置
     * @param val
     */
    public void reSet(int val) {
        if(val<0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        int ArrayIndex=val/8;
        int BitIndex=val%8;
        if(ArrayIndex>elem.length-1) {
            elem = Arrays.copyOf(elem,ArrayIndex+1);
        }
        elem[ArrayIndex]&=~(1<<BitIndex);
        CapCity--;
    }
    public int getCapCity() {
        return CapCity;
    }
}
